import { fileURLToPath, URL } from "node:url"

import { defineConfig, loadEnv } from "vite"
import vue from "@vitejs/plugin-vue"
import vueDevTools from "vite-plugin-vue-devtools"

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), "")
  const apiTarget = env.VITE_API_PROXY_TARGET || "http://localhost:3012"

  return {
    plugins: [vue(), vueDevTools()],
    resolve: {
      alias: {
        "@": fileURLToPath(new URL("./src", import.meta.url)),
      },
    },
    server: {
      port: 8092, // 移动端管理页面使用不同端口
      host: "0.0.0.0", // 允许外部访问，便于移动设备测试
      proxy: {
        "/api": {
          target: apiTarget,
          changeOrigin: true,
        },
        "/files": {
          target: apiTarget, // 代理静态文件
          changeOrigin: true,
        },
      },
    },
  }
})
